package cn.hqc.order.service.impl;


import cn.hqc.basic.service.impl.BaseServiceImpl;
import cn.hqc.msg.domain.SearchMasterMsg;
import cn.hqc.msg.mapper.SearchMasterMsgMapper;
import cn.hqc.order.domain.OrderPetAcquisition;
import cn.hqc.order.dto.ConfirmDto;
import cn.hqc.order.service.IOrderPetAcquisitionService;
import cn.hqc.pet.domain.Pet;
import cn.hqc.pet.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-05-14
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private SearchMasterMsgMapper msgMapper;
    @Autowired
    private PetMapper petMapper;
    /**
     * 取消订单
     * @param orderId
     */
    @Override
    public void cancel(Long orderId) {
        // 先根据orderId 查询出来 order数据
        OrderPetAcquisition order = this.findById(orderId);
        // 修改msg状态 目前是已接单 审核通过 并且 把msg的shopId 置空
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(order.getMsgId());
        msg.setState(1);
        msg.setShopId(null);
        msgMapper.updateByMsg(msg);
        // 修改 order的状态 为已取消
        order.setState(-1);
        this.update(order);
        // 删除pet数据
       petMapper.delete(order.getPetId());
    }

    /**
     * 确认订单
     * @param dto
     */
    @Override
    public void confirm(ConfirmDto dto) {
        // 查询一下
        OrderPetAcquisition order = this.findById(dto.getId());
        // 订单状态、价格、支付方式 已支付
        order.setState(1);
        order.setPayType(dto.getPayType());
        order.setPrice(new BigDecimal(dto.getMoney()));
        this.update(order);
        // pet的 成本价要改
        Pet pet = new Pet();
        pet.setId(order.getPetId());
        pet.setCostprice(new BigDecimal(dto.getMoney()));
        petMapper.update(pet);
        // msg的状态 为已完成
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(order.getMsgId());
        msg.setState(4);
        msgMapper.update(msg);
    }

    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"1");
        map.put(1,"1");
        map.put(1,"2");
    }
}
